<?php

namespace app\api\model;

use think\Model;
use traits\model\SoftDelete;

class CircleComments extends Model
{
    use SoftDelete;
    protected $autoWriteTimestamp = 'int';
    protected $deleteTime = 'deletetime';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $hidden = ['deletetime', 'createtime', 'updatetime', 'online'];

    public function replies()
    {
        return $this->hasMany(CircleCommentReplies::class, 'comment_id', 'id')->where('online', 'yes');
    }

    public function user()
    {
        return $this->hasOne(Users::class, 'id', 'user_id');
    }

    public static function getCommentsByCircelId($circle_id, $length = 15)
    {
        return self::with(['user', 'replies.user'])->where('online', 'yes')->where('circle_id', $circle_id)->order('createtime desc')->paginate($length);
    }
}
